package jvs.vfs.tools;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import x.java.io.File;
import x.javax.swing.filechooser.FileSystemView;
/**
 * 
 * @author Eric Armstrong, Tom Santos, and Steve Wilson
 *
 */
public class FileSystemTreePanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTree tree;

	public FileSystemTreePanel(FileExplorer fe,FileSystemModel model) {
		tree = new FileTree(fe,model) {
			private static final long serialVersionUID = 1L;

			public String convertValueToText(Object value, boolean selected,
					boolean expanded, boolean leaf, int row, boolean hasFocus) {
				return ((File) value).getName();
			}

		};

		tree.setRootVisible(true);
		tree.setShowsRootHandles(true);
		tree.putClientProperty("JTree.lineStyle", "Angled");
		tree.setCellRenderer(new DefaultTreeCellRenderer() {
			private static final long serialVersionUID = 1L;
			private FileSystemView fsv = (FileSystemView)FileSystemView.getFileSystemView();

			public Component getTreeCellRendererComponent(JTree tree,
					Object value, boolean sel, boolean expanded, boolean leaf,
					int row, boolean hasFocus) {
				Component rc = super.getTreeCellRendererComponent(tree, value,
						sel, expanded, leaf, row, hasFocus);
				
				if (rc instanceof JLabel && value instanceof File) {
					JLabel l = (JLabel) rc;
					File f = (File) value;
					l.setText(fsv.getSystemDisplayName(f));
					l.setIcon(fsv.getSystemIcon(f));
				}
				return rc;
			}
		});

		setLayout(new BorderLayout());
		add(tree, BorderLayout.CENTER);
	}

	public JTree getTree() {
		return tree;
	}
}
